package com.zhang.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

/**
 * jedis哨兵用法
 */
public class MySentinelPool {
    private static JedisSentinelPool jedisSentinelPool;

    public static Jedis getResource() {
        if (jedisSentinelPool == null) {
            //创建一个连接池配置
            JedisPoolConfig poolConfig = new JedisPoolConfig();

            poolConfig.setMinIdle(2);
            poolConfig.setMaxIdle(5);
            poolConfig.setMaxTotal(10);
            poolConfig.setBlockWhenExhausted(true);
            poolConfig.setMaxWaitMillis(2000);

            poolConfig.setTestOnBorrow(true);

            //连接到哨兵集群
            Set<String> sentinels = new HashSet<>();

            sentinels.add("192.168.5.102:26379");

            jedisSentinelPool = new JedisSentinelPool("mymaster", sentinels, poolConfig);
        }

        return jedisSentinelPool.getResource();
    }
}
